#!/usr/bin/python3
from utils import String_Util, My_Util2, Type_Mapping
from wrappers.Annotation_Wrapper import annotation
from wrappers.OpenAPI_Wrapper import openapi
from wrappers.Single_Verify_Wrapper import single_verify


class Entity_Field:
    """
    实体属性
    """

    def __init__(
        self,
        context,
        entity_name,
        column_name: str,
        sql_type: str,
        comment: str,
        not_null: str,
        single_verify: str,
        i18n: str,
        unique: str,
        currency: str,
    ) -> None:
        self.context = context
        self.entity_name = entity_name
        self.column_name: str = column_name
        self.sql_type: str = sql_type
        self.name: str = String_Util.field_name(self.column_name)
        self.comment: str = String_Util.check_comment_symbol(comment)
        self.not_null: bool = not_null == "Y"
        self.single_verify: bool = single_verify == "Y"
        self.i18n: bool = i18n == "国际化名称"
        self.unique: bool = unique == "Y"
        self.currency: bool = currency == "金额"
        self.ignores = ["id", "waitDelete"] + String_Util.ignore_fields
        self.my_util2 = My_Util2.My_Util(self.context)

        self.java_type = Type_Mapping.get_java_type(self.sql_type)

    def ignore(self, ignore_list=None):
        if ignore_list is not None:
            return self.name in ignore_list
        else:
            return self.name in self.ignores

    def required(self):
        return self.not_null

    def is_sort(self):
        if self.column_name == "sort_number":
            return True
        else:
            return False

    def is_status(self):
        if self.column_name == "status":
            return True
        else:
            return False

    def is_single_verify(self):
        return self.single_verify

    def code_field(self):
        name = self.name
        java_type = self.java_type
        return f"""private {java_type} {name};"""

    @annotation
    @single_verify
    @openapi(type="field")
    def code_field_with_openapi(self, verify):
        """
        携带OpenAPI的属性代码
        """
        name = self.name
        java_type = self.java_type
        return f"""private {java_type} {name};"""

    def code(self, openapi=True, verify=True):
        if openapi:
            return self.code_field_with_openapi(verify)
        else:
            return self.code_field()
